package com.gg.registration.cmn.controller;

import com.gg.common.result.CommonResult;
import com.gg.common.result.ResultCodeEnum;
import com.gg.registration.cmn.service.DictService;
import com.gg.registration.model.cmn.Dict;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 数据字典
 *
 * @author chenjunjia
 * @date 2022/4/6 22:00
 */
@RestController
@RequestMapping(value = "/admin/cmn")
@CrossOrigin
public class DictController {

    @Resource
    private DictService service;

    /**
     * 通过id获取孩子字段
     * @param id 字段id值
     * @return 统一返回结果
     */
    @GetMapping("getChildById/{id}")
    public CommonResult getChildById(@PathVariable("id") Long id) {
        List<Dict> dictList = service.getChildById(id);
        return null!=dictList ? new CommonResult(ResultCodeEnum.SUCCESS,dictList) : CommonResult.fail();
    }

    /**
     * 查询数据字典中的名字
     * @param dictCode 编码
     * @param value 值
     * @return 统一返回结果
     */
    @GetMapping("getName/{dictCode}/{value}")
    public CommonResult getName(@PathVariable("dictCode") String dictCode,
                                @PathVariable("value") String value) {
        String name = service.getName(dictCode,value);
        return null!=name? new CommonResult(ResultCodeEnum.SUCCESS,name) : CommonResult.fail();
    }


    /**
     * 查询数据字典中的名字
     * @param value 值
     * @return 统一返回结果
     */
    @GetMapping("getNameByValue/{value}")
    public CommonResult getName(@PathVariable("value") String value) {
        String name = service.getName("",value);
        return null!=name? new CommonResult(ResultCodeEnum.SUCCESS,name) : CommonResult.fail();
    }

    /**
     * 根据dictCode查询子成员
     * @param dictCode dict类型
     * @return 统一返回结果集
     */
    @GetMapping("getChildByDictCode/{dictCode}")
    public CommonResult getChildByDictCode(@PathVariable("dictCode") String dictCode) {
        List<Dict> dictList=  service.getChildByDictCode(dictCode);
        return null!=dictList? new CommonResult(ResultCodeEnum.SUCCESS,dictList) : CommonResult.fail();
    }

}
